package zhupf.test.designPattern.prototype.deepClone;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.*;

@Data
@Accessors(chain = true)
public class Person implements Cloneable,Serializable {


    private Integer age;

    private String name;

    private Person friend;

    @Override
    protected Object clone() throws CloneNotSupportedException {

        Person person = null;

        ByteArrayOutputStream bos = null;

        ObjectOutputStream oos = null;

        ByteArrayInputStream bis = null;

        ObjectInputStream ois = null;

        try {
            bos = new ByteArrayOutputStream();

            oos = new ObjectOutputStream(bos);

            oos.writeObject(this);

            bis = new ByteArrayInputStream(bos.toByteArray());
            ois =  new ObjectInputStream(bis);

            person = (Person) ois.readObject();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
           try {
               bos.close();
               bis.close();
               ois.close();
               oos.close();
           }catch (Exception e2){
               e2.printStackTrace();
               return null;
           }
        }
        return person;
    }
}
